

package com.embedsky.administrator.mycardemulation.message;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.embedsky.administrator.mycardemulation.R;

import java.util.List;

/**
 * @Des:
 * @Author: hang.zhao
 * @Date: 2023/2/28
 */
public class MessageAdapter extends RecyclerView.Adapter<MyViewHolder> {

    private List<SocketMessage> list;

    @Override
    public int getItemViewType(int position) {
        SocketMessage socketMessage = list.get(position);
        return socketMessage.getType();
    }

    public MessageAdapter(List<SocketMessage> list) {
        this.list = list;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == SocketMessage.MESSAGE_RECEIVE) {
            View leftView = LayoutInflater.from(parent.getContext()).inflate(R.layout.msg_left_item, parent, false);
            return new LeftViewHolder(leftView);
        }
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.msg_right_item, parent, false);
        return new RightViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        SocketMessage message = list.get(position);
        Log.d("xhh", "onBindViewHolder: " + message);
        if (holder instanceof LeftViewHolder) {
            ((LeftViewHolder) holder).getLeftMsg().setText(message.getData());
        }
        if (holder instanceof RightViewHolder) {
            ((RightViewHolder) holder).getRightMsg().setText(message.getData());
        }
    }

    @Override
    public int getItemCount() {
        if (list == null) {
            return 0;
        }
        return list.size();
    }
}